using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using MyPhotoIndex.Controls;
using System.Drawing.Drawing2D;

namespace MyPhotoIndex.Plugins.WebCommunity
{
    public partial class HtmlCopyForm : FormTemplate
    {
        private string m_attachmentId;
        private int[] m_imageIds;
        private string m_htmlCode;

        public HtmlCopyForm(String attachmentId, int[] imageIds)
        {
            InitializeComponent();

            this.Load += new EventHandler(HtmlCopyForm_Load);
            this.copyButton.Click += new EventHandler(copyButton_Click);
            
            panel2.Paint += new PaintEventHandler(headerPanel_Paint);

            m_attachmentId = attachmentId;
            m_imageIds = imageIds;
        }

        void headerPanel_Paint(object sender, PaintEventArgs e)
        {
            Graphics graphics = e.Graphics;

            Rectangle gradDrawingRect = panel2.ClientRectangle;
            Rectangle drawingRect = panel2.ClientRectangle;

            gradDrawingRect.Height -= 2;

            if (gradDrawingRect.Height <= 0 ||
                gradDrawingRect.Width <= 0)
            {
                return;
            }

            using (Brush parentBrush = new LinearGradientBrush(gradDrawingRect,
            Color.FromArgb(222, 236, 253), Color.FromArgb(171, 191, 216), LinearGradientMode.Vertical))
            {
                graphics.FillRectangle(parentBrush, gradDrawingRect);
            }

            using (Pen pen = new Pen(Color.FromArgb(108, 131, 158)))
            {
                Point start = new Point(0, drawingRect.Height - 2);
                Point end = new Point(drawingRect.Width - 1, drawingRect.Height - 2);
                graphics.DrawLine(pen, start, end);
            }

            using (Brush gradBrush = new LinearGradientBrush(gradDrawingRect,
                Color.FromArgb(222, 236, 253), Color.FromArgb(108, 131, 158), LinearGradientMode.Vertical))
            {
                using (Pen pen = new Pen(gradBrush))
                {
                    Point start = new Point(0, 0);
                    Point end = new Point(0, drawingRect.Height - 1);
                    graphics.DrawLine(pen, start, end);

                    start = new Point(drawingRect.Width - 2, 0);
                    end = new Point(drawingRect.Width - 2, drawingRect.Height - 1);
                    graphics.DrawLine(pen, start, end);
                }
            }

            using (Pen pen = new Pen(Color.FromArgb(79, 94, 113)))
            {
                Point start = new Point(0, drawingRect.Height - 1);
                Point end = new Point(drawingRect.Width, drawingRect.Height - 1);
                graphics.DrawLine(pen, start, end);

                start = new Point(drawingRect.Width - 1, 0);
                end = new Point(drawingRect.Width - 1, drawingRect.Height - 1);
                graphics.DrawLine(pen, start, end);
            }
        }

        void copyButton_Click(object sender, EventArgs e)
        {
            HtmlFragment.CopyToClipboard(m_htmlCode, String.Empty, new Uri("http://www.piccue.com"));
        }

        void HtmlCopyForm_Load(object sender, EventArgs e)
        {
            updateHtml();
        }

        private void updateHtml()
        {
            StringBuilder sb = new StringBuilder();
            foreach (int imageId in m_imageIds)
            {
                sb.Append("&nbsp;");
                sb.AppendFormat("<A href=\"http://www.piccue.com/viewer/photoviewer.aspx?attachmentid={0}\" target=_blank spellChecked=\"true\">", m_attachmentId);
                sb.AppendFormat("<IMG src=\"http://media.piccue.com/{0}/preview/{1}.gif\" height=\"100px\" border=\"0\" spellChecked=\"true\">", m_attachmentId, imageId);
                sb.Append("</A>");
            }

            m_htmlCode = sb.ToString();
            this.webBrowser1.DocumentText = m_htmlCode;
        }

        private void radioButton1_CheckedChanged(object sender, EventArgs e)
        {
            //if (radioButton1.Checked == true)
            //{
            //    updateHtml();
            //}
            //else
            //{
            //    updateHtml1();
            //}
        }

        private void updateHtml1()
        {
            //StringBuilder sb = new StringBuilder();
            //for (int i = 0; i < m_imageCount; i++)
            //{
            //    sb.Append(FormatOneImageHtml(i));
            //}

            //m_htmlCode = sb.ToString();
            //this.webBrowser1.DocumentText = m_htmlCode;
            
        }

        private string FormatOneImageHtml(int imageId)
        {
            StringBuilder sb = new StringBuilder();

            sb.Append("<table style=\"width: 150px;border:0px; background-position: left top; background-image: url(http://www.myphotoindex.com/images/frames/frame1.jpg); background-repeat: no-repeat; height: 126px; background-color: transparent;>\"");
            sb.Append("<tbody>");
            sb.Append("<tr>");
            sb.Append("<td>");
       //     sb.AppendFormat("<IMG src=\"{0}\"", topLeftImage);
            sb.Append("</td>");
            sb.Append("<td>");
       //     sb.AppendFormat("<IMG src=\"{0}\"", topImage);
            sb.Append("</td>");
            sb.Append("<td>");
        //    sb.AppendFormat("<IMG src=\"{0}\"", topRightImage);
            sb.Append("</td>");
            sb.Append("</tr>");
            sb.Append("<tr>");
            sb.Append("<td>");
       //     sb.AppendFormat("<IMG src=\"{0}\"", leftImage);
            sb.Append("</td>");
            sb.Append("<td>");
            sb.Append("<p align=\"center\">");
            sb.AppendFormat("<A href=\"http://media.piccue.com/{0}/preview/%{0}.gif\" target=_blank spellChecked=\"true\">", m_attachmentId);
            sb.AppendFormat("<IMG src=\"http://www.myphotoindex.com/GetImage.ashx?attachmentId={0}&imageindex={1}&isThumbnail=true\" border=0 spellChecked=\"true\">", m_attachmentId, imageId);
            sb.Append("</A>");
            sb.Append("</p>");
            sb.Append("</td>");
            sb.Append("<td style=\"height: 1%\">");
         //  sb.AppendFormat("<IMG src=\"{0}\"", rightImage);
            sb.Append("</td>");
            sb.Append("</tr>");
            sb.Append("<tr>");
            sb.Append("<td>");
         //   sb.AppendFormat("<IMG src=\"{0}\"", bottomLeftImage);
            sb.Append("</td>");
            sb.Append("<td>");
         //   sb.AppendFormat("<IMG src=\"{0}\"", bottomImage);
            sb.Append("</td>");
            sb.Append("<td>");
          //  sb.AppendFormat("<IMG src=\"{0}\"", bottomRightImage);
            sb.Append("</td>");
            sb.Append("</tr>");
            sb.Append("</tbody>");
            sb.Append("</table>");

            string html = sb.ToString();
            return html;
        }

        private void webBrowser1_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
        {

        }
    }
}